package com.logicwin.aibala.ui.screens.screenmain;

import com.logicwin.aibala.R;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.logicwin.aibala.detailPackActivity.testActivity;
import com.logicwin.aibala.model.androidSWInfo.APKListItem;

public class ApkPackageAdapter extends BaseAdapter implements AdapterView.OnItemClickListener{
	private final int mHighestLevel = 5;
	private final int mLowestLevel  = 0;
	protected Activity mParentActivity;
	protected List<APKListItem> mApkList;
	public ApkPackageAdapter(Activity activity, List<APKListItem> apkList){
		mParentActivity = activity;
		mApkList = apkList;
	}
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		APKListItem item;
		
		if (view == null) {		
			view = mParentActivity.getLayoutInflater().inflate(R.layout.package_item, null);
		}
		if ((item = this.mApkList.get(position)) == null) {
			return view;
		}
		inflateItemView(view, item);
		return view;
	}	
	public void inflateItemView(View view, APKListItem item){
		ImageView packimage = (ImageView) view.findViewById(R.id.package_image);
//		if(item.getIcon() != null) packimage.setImageBitmap(item.getIcon());
		packimage.setImageResource(item.getIconResID());
		
		((TextView) view.findViewById(R.id.package_name)).setText(item.getAPKName());
		((TextView) view.findViewById(R.id.package_size)).setText(item.getAPKSize());
		((TextView) view.findViewById(R.id.package_times)).setText(item.getDownloadCount());
		
		inflateLevel(view, item.getRefStarCount());		
	}
	private void inflateLevel(View view, int level){
		if(level <mLowestLevel || level >mHighestLevel) 
			Log.w("ScreenHomeAdapter", "mLevel is out of range, mLevel value is "+ level);
		if(level<mLowestLevel) level = mLowestLevel;
		if(level>mHighestLevel) level = mHighestLevel;
		ImageView imagestars[] = new ImageView[mHighestLevel];
		imagestars[0]  = (ImageView) view.findViewById(R.id.star_image1);
		imagestars[1]  = (ImageView) view.findViewById(R.id.star_image2);
		imagestars[2]  = (ImageView) view.findViewById(R.id.star_image3);
		imagestars[3]  = (ImageView) view.findViewById(R.id.star_image4);
		imagestars[4]  = (ImageView) view.findViewById(R.id.star_image5);
		int idex = 0;
		for(; idex <level; idex++){
			imagestars[idex].setImageDrawable(mParentActivity.getResources().getDrawable(R.drawable.star_true));
		}
		for(;idex < mHighestLevel; idex++){
			imagestars[idex].setImageDrawable(mParentActivity.getResources().getDrawable(R.drawable.star_false));
		}		
	}
	public List<APKListItem> getCurrApkList(){
		return mApkList;
	}
	public void setCurrApkList(List<APKListItem> apklist){
		mApkList = apklist;
	}
	public Activity getParentActivity(){
		return mParentActivity;
	}
	@Override
	public int getCount() {
		return mApkList.size();
	}
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mApkList.get(position);
		//return null;
	}
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		Log.i("ApkPackageAdapter", mApkList.get(position).getAPKName());
		Intent intent = new Intent(mParentActivity, testActivity.class);
			
		intent.putExtra("resId", mApkList.get(position).getDetailResID());
		mParentActivity.startActivity(intent);
		
	}	
}
